package com.swolin.ThreadDemo;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class TestCallable implements Callable<Integer> {
    public static void main(String[] args) {
        TestCallable tc = new TestCallable();
        FutureTask<Integer> task = new FutureTask<Integer>(tc);

        for(int i = 0; i < 100; i++){
            System.out.println(Thread.currentThread().getName() + "循环变量的值：" + i);
            if(i == 20){
                new Thread(task).start();
            }
        }

        try {
            System.out.println("子线程的返回值："+ task.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }



    @Override
    public Integer call() throws Exception {
        int i = 0;
        for(; i < 100; i++){
            System.out.println(Thread.currentThread().getName() + " " + i);
        }
        return i;
    }
}